package com.csu.cphone.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.csu.common.utils.JSONUtil;
import com.csu.common.utils.JsonUtils;
import com.csu.common.utils.WebUtils;
import com.csu.cphone.pojo.PhoneBrandPojo;
import com.csu.cphone.pojo.PhonePojo;
import com.csu.cphone.service.PhoneService;
import com.csu.cphone.vo.HotPhone;
import com.csu.cphone.vo.ResultVo;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @项目名称：cphone
 * @类名称：PhoneController
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月13日 下午4:09:53
 * @company:
 */
@Controller
@RequestMapping("/phone")
@Api(value = "base", description = "基础信息")
public class PhoneController
{

    @Autowired
    private PhoneService phoneService;

    @ResponseBody
    @ApiOperation(value = "热门机型", notes = "首页展示热门机型列表")
    @RequestMapping(value = "/hotRecycles", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<HotPhone>> hotRecycles(HttpServletRequest request, HttpServletResponse response)
    {
        ResultVo<List<HotPhone>> result = new ResultVo<List<HotPhone>>();

        List<HotPhone> hotList = phoneService.getHotRecycleList();

        result.setData(hotList);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "获取手机品牌", notes = "获取手机品牌列表")
    @RequestMapping(value = "/getBrands", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<PhoneBrandPojo>> getBrands(HttpServletRequest request, HttpServletResponse response)
    {
        ResultVo<List<PhoneBrandPojo>> result = new ResultVo<List<PhoneBrandPojo>>();

        List<PhoneBrandPojo> brandList = phoneService.getBrandList();
        result.setData(brandList);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "获取可回收机型", notes = "获取可回收机型列表")
    @RequestMapping(value = "/getRecyclePhones", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<PhonePojo>> getRecyclePhones(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "品牌id") @RequestParam(value = "brandId", required = true) int brandId)
    {
        ResultVo<List<PhonePojo>> result = new ResultVo<List<PhonePojo>>();

        List<PhonePojo> recycleList = phoneService.getRecyclePhoneList(brandId);

        result.setData(recycleList);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "添加品牌", notes = "添加手机品牌")
    @RequestMapping(value = "/addPhoneBrand", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public String addPhoneBrand(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "callback") @RequestParam(value = "callback", required = false) String callback,
            @ApiParam(value = "f") @RequestParam(value = "f", required = false) String f,
            @ApiParam(value = "品牌名称") @RequestParam(value = "brandName", required = true) String brandName,
            @ApiParam(value = "图片路径") @RequestParam(value = "url", required = true) String url)
    {
        PhoneBrandPojo phoneBrand = phoneService.addBrand(brandName, url);
        String jsonString = JSONUtil.toJSONString(phoneBrand);
        return WebUtils.returnJsonP(callback, jsonString, request);
    }

    @ResponseBody
    @ApiOperation(value = "添加手机", notes = "添加手机")
    @RequestMapping(value = "/addPhone", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public String addPhone(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "callback") @RequestParam(value = "callback", required = false) String callback,
            @ApiParam(value = "f") @RequestParam(value = "f", required = false) String f,
            @ApiParam(value = "品牌id") @RequestParam(value = "brandId", required = true) int brandId,
            @ApiParam(value = "手机名称") @RequestParam(value = "name", required = true) String name,
            @ApiParam(value = "图片路径") @RequestParam(value = "url", required = true) String url,
            @ApiParam(value = "是否用于回收:0不用,1用") @RequestParam(value = "isUseRecycle", required = true) int isUseRecycle,
            @ApiParam(value = "是否用于维修:0不用,1用") @RequestParam(value = "isUsemaintain", required = true) int isUsemaintain,
            @ApiParam(value = "是否用于热门回收:0不用,1用") @RequestParam(value = "isUseHotRecycle", required = true) int isUseHotRecycle,
            @ApiParam(value = "最高回收价格") @RequestParam(value = "hotPrice", required = false) double hotPrice)
    {

        PhonePojo phone = phoneService.addPhone(brandId, name, url, isUseRecycle, isUsemaintain);
        if (isUseHotRecycle == 1)
        {
            HotPhone hotPhone = new HotPhone();
            hotPhone.setHotPrice(hotPrice);
            hotPhone.setModelId(phone.getId());
            hotPhone.setName(name);
            hotPhone.setUrl(url);
            phoneService.addToHotRecycleList(hotPhone);
        }
        String jsonString = JSONUtil.toJSONString(phone);
        return WebUtils.returnJsonP(callback, jsonString, request);
    }

    @ResponseBody
    @ApiOperation(value = "添加热门机型", notes = "添加热闷回收机型")
    @RequestMapping(value = "/addHotRecycle", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
    public String addHotRecycle(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "callback") @RequestParam(value = "callback", required = false) String callback,
            @ApiParam(value = "f") @RequestParam(value = "f", required = false) String f,
            @ApiParam(value = "机型id") @RequestParam(value = "modelId", required = true) int modelId,
            @ApiParam(value = "最高回收价格") @RequestParam(value = "hotPrice", required = true) double hotPrice,
            @ApiParam(value = "手机名称") @RequestParam(value = "name", required = true) String name,
            @ApiParam(value = "图片路径") @RequestParam(value = "url", required = true) String url)
    {

        HotPhone hotPhone = new HotPhone();
        hotPhone.setHotPrice(hotPrice);
        hotPhone.setModelId(modelId);
        hotPhone.setName(name);
        hotPhone.setUrl(url);
        phoneService.addToHotRecycleList(hotPhone);
        return WebUtils.returnJsonP(callback, JSONUtil.toJSONString(hotPhone), request);
    }

    @ResponseBody
    @ApiOperation(value = "上传图片", notes = "获得图片路径")
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public String uploadFile(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "callback") @RequestParam(value = "callback", required = false) String callback,
            @ApiParam(value = "f") @RequestParam(value = "f", required = false) String f,
            @ApiParam(value = "file对象") @RequestParam(value = "productFile", required = true) CommonsMultipartFile productFile)
            throws IOException
    {
        if (request.getCharacterEncoding() == null)
        {
            request.setCharacterEncoding("UTF-8");// 你的编码格式
        }

        String requestUrl = phoneService.wirteFile(productFile);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("url", requestUrl);
        String json = JsonUtils.obj2JsonStr(params);
        return WebUtils.returnJsonP(callback, json, request);
    }

}
